﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

using Mvc.XForms.Utilities;
using Mvc.XForms.Controls.Settings;

namespace Mvc.XForms.Controls {
	public abstract class ContainerControlBase<TContext> : XFormControlBase, IContainerControl< TContext> {
		public IXForm XForm { get; set; }
		public TContext ModelContext { get; set; }
		public string Prefix { get; set; }
		public IList<IXFormControl> Controls { get; set; }
		public bool IsInRepeatTemplate { get; set; }
		public bool SetChildAsInRepeatTemplate { get; set; }

		public ContainerControlBase(IContainerControl parent, Expression bind) : base(parent, null, bind) {
			if (parent != null) {
				XForm = parent.XForm;
				SetChildAsInRepeatTemplate = parent.SetChildAsInRepeatTemplate;
				IsInRepeatTemplate = SetChildAsInRepeatTemplate;

				if (IsInRepeatTemplate)
					parent.Controls.Add(this);
			}

			Controls = new List<IXFormControl>();
		}

		public abstract void Render();

		public PartialSettings<TContext, T> Partial<T>(Expression<Func<TContext, T>> bind) {
			return new PartialSettings<TContext, T>(this, bind);
		}
	}
}